package com.shadow.baiqun.delegate.receive;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.shadow.baiqun.R;
import com.shadow.baiqun.R2;
import com.shadow.baiqun.config.ItemField;
import com.shadow.baiqun.delegate.receive.adapter.SupplierListAdapter;
import com.shadow.baiqun.delegate.receive.pregoods.GoodsListDelegate;
import com.shadow.baiqun.view.TableHeadView;
import com.shadow.core.delegate.bottom.BottomItemDelegate;
import com.shadow.core.event.StartBrotherFragmentEvent;
import com.shadow.core.net.callback.IError;
import com.shadow.core.net.callback.ISuccess;
import com.shadow.core.net.rx.RxRestClient;
import com.shadow.ui.recycler.MultipleItemEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author zy
 * on 2018/6/9
 */
public class SupplierDelegate extends BottomItemDelegate {
    @BindView(R2.id.lv_title)
    TableHeadView mHeadView;
    @BindView(R2.id.swipe_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private SupplierListAdapter mAdapter;

    @Override
    public Object setLayout() {
        return R.layout.delegate_supplier;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        List<String> list = new ArrayList<>();
        list.add("供应商名称");
        list.add("供应商代码");
        list.add("供应商地址");
        list.add("所属机构");
        mHeadView.setHeaders(list);
        mAdapter = new SupplierListAdapter(getContext(), null);
        mAdapter.openLoadAnimation();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                MultipleItemEntity multipleItemEntity = (MultipleItemEntity) adapter.getItem(position);
                if (multipleItemEntity != null) {
                    SupplierEntry supplierEntry= multipleItemEntity.getField(ItemField.ITEM_OBJ);
                    EventBus.getDefault().post(new StartBrotherFragmentEvent(
                            GoodsListDelegate.create()));
                }
            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        RxRestClient.builder()
                .url("WMS/backstage/collectgoods/getSupplier")
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        SupplierConverter supplierConverter = new SupplierConverter();
                        supplierConverter.setJsonData(response);
                        ArrayList<MultipleItemEntity> itemEntities = supplierConverter.convert();
                        if(itemEntities != null) {
                            mAdapter.setNewData(itemEntities);
                        }
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                    }
                })
                .build()
                .unRestGet();
    }
}
